package com.sxt.practiceapplication.utils.okhttp;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.base.BasePager;
import com.sxt.practiceapplication.utils.LogUtil;

import java.io.IOException;

import okhttp3.Call;

public abstract class DefaultRealCallback implements OkHttpUtils.RealCallback {

    private final BasePager basePager;

    private final Context context;

    public DefaultRealCallback(BasePager basePager, Context context) {
        this.basePager = basePager;
        this.context = context;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
        FrameLayout content = basePager.content;
        View error = View.inflate(context, R.layout.network_error_layout, null);
        if(content != null) {
            error.setOnClickListener(v -> {
                content.removeView(error);
                content.requestLayout();
                basePager.reloadData();
            });
            content.addView(error);
        }

        LogUtil.e(e.getMessage());
    }
}
